var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marker: [],
    longitude: "113.324520",
    latitude: "23.099994"
  },
  onLoad: function (options) {
    var that = this;
    var lat = options.lat;
    var info = options.info;
    var lng = options.lng;
    var shopId = options.shopId;
    that.setData({
      longitude: lng,
      latitude: lat,
      info: info,
      id: shopId
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
        })
      }
    })
  },
  onShow: function () {
    var that = this;
    that.getShopList();
  },
  onHide:function(){

  },

  // locationCol: function (e) {
  //   var index = e.currentTarget.dataset.index;
  //   var lat = this.data.dataList[index].lat;
  //   var lng = this.data.dataList[index].lng;
  //   console.log(e);
  //   this.setData({
  //     longitude: lng,
  //     latitude: lat
  //   })
  // },

  //点击标记点触发
  markerTap: function (e) {
    var that = this;
    var markerId = e.markerId;
    var shop = that.data.dataList[markerId];
    for (var i=0;i<that.data.dataList.length;i++){
      if (that.data.dataList[i].shop_id == markerId){
        that.setData({
          shopName: that.data.dataList[i].shop_name,
          address: that.data.dataList[i].address,
          distance: that.data.dataList[i].distance,
          shopId: that.data.dataList[i].shop_id
        })
      }
    }
  },

  //筛选店铺
  selectShop:function(e){
    var shopid = e.currentTarget.dataset.shopid;
    this.setData({
      id: shopid
    })
    wx.setStorageSync('shopId', shopid)
  },

  //获取店铺列表
  getShopList: function () {
    var that = this;
    var params = new Object()
    params.info = this.data.info;
    // params.lng = this.data.lng;
    // params.lat = this.data.lat;
    //获取经纬度
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        params.lat = res.latitude
        params.lng = res.longitude
        //商家列表请求
        api.POST({
          params: params,
          url: app.globalData.base_url + 'shoppingcart/shopList',
          success: function (res) {
            if (util.isEmpty(res.data.code)) {
              var list = res.data.data;
              var id = that.data.id;
              for (var i = 0; i < list.length; i++) {
                if (list[i].shop_id == id) {
                  that.setData({
                    shopName: list[i].shop_name,
                    address: list[i].address,
                    distance: list[i].distance,
                    shopId: list[i].shop_id
                  })
                }
              }


              var markers = [];
              if (list.length > 0) {
                for (var i = 0; i < list.length; i++) {

                  var marker = {
                    id: list[i].shop_id,
                    latitude: list[i].lat,
                    longitude: list[i].lng,
                    iconPath: '../../images/ic_point.png',
                    width: 21,
                    height: 32
                  }

                  markers.push(marker)
                }
                that.setData({
                  dataList: res.data.data,
                  markers: markers
                })
              }
            }
          },
          fail: function (fail) {
            util.showError("登录失败，请检查网络");

          }
        })
      }
    })
  }
})